package guilayer;

import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class MainContent extends JPanel {
	
	private JPanel jpnlSale, jpnlCustomer, jpnlProduct;
	private JTabbedPane tabbedPane;
	private int tabIndex;

	/**
	 * Create the panel.
	 */
	public MainContent(int switchCase, Object data) {
		tabIndex = Integer.parseInt((""+switchCase).substring(0,1));
		
		setLayout(new BorderLayout(0,0));
		
		//((MainWindow)(this.getRootPane().getParent())).promptMsg("hahaha");
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		
		jpnlSale = new SalesContent(null);
		jpnlCustomer = new CustomerOverview();
		jpnlProduct = new ProductOverview();
		switch (switchCase) {
		case 0: // SALE (Standard)
			jpnlSale = new SalesContent((String)data);
			break;
		case 9: // SALE-Order overview
			jpnlSale = new OrderList();
			break;

		case 1: // CUSTOMER (1 Standard overview)
			jpnlCustomer = new CustomerOverview();
			break;
		case 11: // CUSTOMER (.1 New customer
			jpnlCustomer = new CustomerNew();
			break;
		case 12: // CUSTOMER (.2 Lookup customer
			if (data==null) {
				jpnlCustomer = new CustomerLookup();
			} else {
				jpnlCustomer = new CustomerLookup((String)data);
			}
			break;
		case 13: // CUSTOMER (.3 Edit customer
			if (data==null) {
				jpnlCustomer = new CustomerEdit();
			} else {
				jpnlCustomer = new CustomerEdit((String)data);
			}
			break;
		case 14: // CUSTOMER (.4 Delete customer
			if (data==null) {
				jpnlCustomer = new CustomerDelete();
			} else {
				jpnlCustomer = new CustomerDelete((String)data);
			}
			break;
			
		case 2: // STOCK (1 Standard overview)
			jpnlProduct = new ProductOverview();
			break;
		case 21: // STOCK (.1 New product)
			jpnlProduct = new ProductNew();
			break;
		case 22: // STOCK (.2 Lookup product)
			if (data==null) {
				jpnlProduct = new ProductLookup();
			} else {
				jpnlProduct = new ProductLookup((String)data);
			}
			break;
		case 23: // STOCK (.3 Edit product)
			if (data==null) {
				jpnlProduct = new ProductEdit();
			} else {
				jpnlProduct = new ProductEdit((String)data);
			}
			break;
		case 24: // STOCK (.4 Delete product)
			if (data==null) {
				jpnlProduct = new ProductDelete();
			} else {
				jpnlProduct = new ProductDelete((String)data);
			}
			break;
			
		case 3: // RENT
			break;
			
		default:
			break;
		}

		tabbedPane.addTab("Salg", jpnlSale);
		tabbedPane.addTab("Kunde", jpnlCustomer);
		tabbedPane.addTab("Lager", jpnlProduct);
		tabbedPane.addTab("Udlejning", new JPanel().add(new JLabel("This section is not done yet!")));
		
		if (tabIndex==9) {
			tabIndex = 0;
		}
		tabbedPane.setSelectedIndex(tabIndex);
		
		add(tabbedPane, BorderLayout.CENTER);
		

		
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				int switchCase = tabbedPane.getSelectedIndex();
				
				onChangeSwitchLeftMenu(switchCase);
				
				/*switch (switchCase) {
				case 0: // SALE
					jpnlSale = new SalesContent();
					break;

				case 1: // CUSTOMER
					jpnlCustomer = new LeftMenu(1);
					break;
					
				case 2: // STOCK
					break;
					
				case 3: // RENT
					break;
					
				default:
					break;
				}*/
			}
		});
		

	}
	
	private void onChangeSwitchLeftMenu(int menuItemIndex)
	{
		JRootPane pane = this.getRootPane();
		Container frame = pane.getParent();
		
		MainWindow window = (MainWindow) frame;
		
		window.switchLeftMenu(menuItemIndex);
	}
}
